package com.yunxi.service.pjbx.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.pojo.pjbx.InsuranceVO;
import com.yunxi.eai.service.pjbx.PjbxService;
import com.yunxi.hubToBiz.pojo.OrderActualSalesReqVO;
import com.yunxi.hubToBiz.service.OrderActualSales2InsuranceDubboService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by Administrator on 2019/9/9.
 */
@Service
@Qualifier("pjbxServiceImpl")
public class PjbxServiceImpl implements PjbxService {
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Autowired
    OrderActualSales2InsuranceDubboService orderActualSales2InsuranceDubboServiceImpl;
    protected static final Logger logger = LoggerFactory.getLogger(PjbxServiceImpl.class);
    @Override
    public ApiResult distributePjbx(EsbVO esbVO) throws Exception {
        String serviceName = esbVO.getServiceName();
        ApiResult apiResult = null;
        if (serviceName.equals(Constant.PJBX_PMS_INSURANCE)) {
            apiResult = findSolidPinInformation(esbVO);
        }else {
            return ResponseUtil.fail(500, "没有对应的业务接口!");
        }
        return apiResult;
    }

    private ApiResult findSolidPinInformation(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setEsbEaiContent(esbVO.getReqBody());
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("凭驾保险查询实销信息");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(eaiLog.getServiceName());
        InsuranceVO insuranceVO= JSONObject.toJavaObject(JSON.parseObject(reqBody),InsuranceVO.class);
        try {
            //内部业务
            OrderActualSalesReqVO orderActualSalesReqVO=new OrderActualSalesReqVO();
            BeanUtils.copyProperties(insuranceVO,orderActualSalesReqVO);
            apiResult=orderActualSales2InsuranceDubboServiceImpl.orderActualSales2Insurance(orderActualSalesReqVO);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode()==-1){
                eaiLog.setDisposeStatus(2);
            }
        } catch (Exception e) {
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
            logger.error(e.getMessage(), e);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return apiResult;
    }
}
